using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace cb0tServer
{
    class AdminLog
    {
        public static void ReportProxy(UserObject userobj)
        {
            StreamWriter s;

            try
            {
                if (!File.Exists(sb0t_winGUI.RunOnceFileUpdater.sbotpath + "data/blacklistlog.txt"))
                {
                    s = File.CreateText(sb0t_winGUI.RunOnceFileUpdater.sbotpath + "data/blacklistlog.txt");
                }
                else
                {
                    s = File.AppendText(sb0t_winGUI.RunOnceFileUpdater.sbotpath + "data/blacklistlog.txt");
                }

                String str = Timestamp() + " " + userobj.name + " " + userobj.physical_ip.ToString();
                s.WriteLine(str);
                s.Flush();
                s.Close();
            }
            catch { }
        }

        public static void WriteLine(String text)
        {
            String str = Timestamp() + " " + text;
            StreamWriter s;

            try
            {
                if (!File.Exists(sb0t_winGUI.RunOnceFileUpdater.sbotpath + "adminlog.txt"))
                {
                    s = File.CreateText(sb0t_winGUI.RunOnceFileUpdater.sbotpath + "adminlog.txt");
                }
                else
                {
                    s = File.AppendText(sb0t_winGUI.RunOnceFileUpdater.sbotpath + "adminlog.txt");
                }

                s.WriteLine(str);
                s.Flush();
                s.Close();
            }
            catch { }

            byte _level = CommandLevels.GetLevel(CommandLevels.LevelCodes.logsend);

            foreach (UserObject u in UserPool.Users)
                if (u.IsSet())
                    if (u.logged_in)
                        if (u.admin || u.host)
                            if (u.logsend)
                                if (u.level >= _level)
                                    u.SendOutboundPacket(ServerOutboundPackets.PMPacket(CoreSettings.room_bot, "LOG: " + str));
        }

        public static void WriteLine(String text, bool log_only)
        {
            StreamWriter s;

            try
            {
                if (!File.Exists(sb0t_winGUI.RunOnceFileUpdater.sbotpath + "adminlog.txt"))
                {
                    s = File.CreateText(sb0t_winGUI.RunOnceFileUpdater.sbotpath + "adminlog.txt");
                }
                else
                {
                    s = File.AppendText(sb0t_winGUI.RunOnceFileUpdater.sbotpath + "adminlog.txt");
                }

                String str = Timestamp() + " " + text;
                s.WriteLine(str);
                s.Flush();
                s.Close();
            }
            catch { }
        }

        public static void Init()
        {
            StreamWriter s;

            try
            {
                if (!File.Exists(sb0t_winGUI.RunOnceFileUpdater.sbotpath + "adminlog.txt"))
                {
                    s = File.CreateText(sb0t_winGUI.RunOnceFileUpdater.sbotpath + "adminlog.txt");
                    s.WriteLine(Timestamp() + " sb0t admin log");
                    s.Flush();
                    s.Close();
                }
            }
            catch { }
        }

        private static String Timestamp()
        {
            DateTime d = DateTime.Now;
            return d.ToShortDateString() + " " + d.ToLongTimeString();
        }
    }
}
